#include <asm/regdef.h>
#include <asm/asm.h>
#include <stackframe.h>
#include <mmu.h>
NESTED(exc_hand_asm, 0, sp)
	csrrw	zero, sscratch, sp
//	la	sp, TIMESTACK
	la	sp, KERNEL_SP
	ld	sp, 0(sp)
	SAVE_ALL
	CLI

//	li	t1, 8 // Code of U-mode ecall
//	beq	t0, t1, handle_sys
	csrr	a1, stval
	csrr	a0, scause
	csrr	a2, sepc
	csrr	a3, satp
	move	a4, sp
	
	csrr	t0, scause
	la	t1, exc_hand_table
	sll	t0, t0, 3
	add	t1, t1, t0
	ld	t1, 0(t1)
	jalr	t1

	/*csrr	t0, scause
	li	t1, 0xf // Cause 15: Store page fault
	beq	t0, t1, PAGE_FAULT

	call	exc_hand_reserved*/
	sret

PAGE_FAULT:
	call	exc_hand_pgfault
END(exc_hand_asm)

.align 3
exc_hand_table:
	.quad	exc_hand_reserved	// 0
	.quad	exc_hand_reserved	// 1
	.quad	exc_hand_reserved	// 2
	.quad	exc_hand_reserved	// 3
	.quad	exc_hand_reserved	// 4
	.quad	exc_hand_reserved	// 5
	.quad	exc_hand_reserved	// 6
	.quad	exc_hand_reserved	// 7
	.quad	handle_sys		// 8
	.quad	exc_hand_reserved	// 9
	.quad	exc_hand_reserved	// 10
	.quad	exc_hand_reserved	// 11
	.quad	exc_hand_reserved	// 12
	.quad	exc_hand_reserved	// 13
	.quad	exc_hand_reserved	// 14
//	.quad	exc_hand_reserved	// 15
	.quad	exc_hand_pgfault
	.quad	exc_hand_reserved	// 16
